package com.myhexin.demo.ignite.test.base;

import com.myhexin.demo.ignite.model.po.Person;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.SqlFieldsQuery;
import org.apache.ignite.client.ClientCache;
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.configuration.ClientConfiguration;

import java.util.List;

/**
 * @author zhangzhonglv@myhexin.com
 * @date 2025/8/24
 */
public class KeyValueAndSQLDemo {

    private static final String IGNITE_ADDR = "127.0.0.1:10800";

    public static void main(String[] args) {
        ClientConfiguration cfg = new ClientConfiguration();
        cfg.setAddresses(IGNITE_ADDR);
        try (IgniteClient client = Ignition.startClient(cfg)) {
            client.query(new SqlFieldsQuery(String.format(
                    "CREATE TABLE IF NOT EXISTS Person2 (id INT PRIMARY KEY, name VARCHAR) WITH " +
                            "\"VALUE_TYPE=%s\", \"KEY_TYPE=%s\", \"CACHE_NAME=%s\"",
                    Person.class.getName(), Integer.class.getName(), "person2Cache")).setSchema("PUBLIC")).getAll();
            ClientCache<Integer, Person> person2Cache = client.cache("person2Cache");
            person2Cache.put(1, new Person(1, "John Doe"));

            String sql = "SELECT * FROM Person2";
            SqlFieldsQuery fieldsQuery = new SqlFieldsQuery(sql);
            try (QueryCursor<List<?>> cursor = person2Cache.query(fieldsQuery)) {
                cursor.forEach(System.out::println);
            }
        }
    }
}
